/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tableCellRender;

import java.awt.Component;
import java.text.DecimalFormat;
import javax.swing.JTable;
import javax.swing.JTextField;
import static javax.swing.SwingConstants.CENTER;
import javax.swing.table.TableCellRenderer;
import otros.Util;

/**
 *
 * @author FERRETERIA
 */
 public class Id implements TableCellRenderer {
    private int filaSel;
    private boolean fuera;
    
    public Id (int fila, boolean fuera) {
        super();
        filaSel = fila;
        this.fuera = fuera;
    }
    
    @Override
    public Component getTableCellRendererComponent(final JTable jtable, Object value, boolean selected, boolean focused, int row, int column) {
        JTextField t = new JTextField();
        t.setBorder(null);
        t.setOpaque(true);
        t.setHorizontalAlignment(CENTER);
        if ( value != null) {
            DecimalFormat df;
            int tipo = (!value.toString().contains("-"))? 1 : 2;
            switch (tipo) {
                case 1:
                    //df = new DecimalFormat("00000000000000000000");
                    df = new DecimalFormat("#0");
                    t.setText(""+df.format(Integer.parseInt(value.toString())));
                    break;
                default:
                    df = new DecimalFormat("000000");
                    int num = Integer.parseInt(value.toString().substring(5));
                    t.setText(value.toString().substring(0,4) +"-"+ df.format(num));
                    break;
            }
            
        } else
            t.setText("");
        
        t.setBackground(Util.getBackground(row,filaSel,jtable, selected, fuera));
        t.setForeground(Util.getForeground(row,filaSel,jtable,selected, fuera));              
        return t;
    }
}
    
